package petstore.web.servlet.order;

import petstore.domain.CartItem;
import petstore.service.LocationService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/address")
public class AddressServlet extends HttpServlet
{
    private static final String ORDER_PAGE = "WEB-INF/jsp/orderPage/orderPage.jsp";
    List<CartItem> orderItemList = new ArrayList<CartItem>();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        HttpSession session = req.getSession();

        orderItemList.clear();
        System.out.println("FIrst step");
        String uid = req.getParameter("uid");
        LocationService locationService = new LocationService(Integer.parseInt(uid));
        System.out.println("uid::" + uid);
        String type = req.getParameter("type");

        if(type.equals("1")){
            String Fs = req.getParameter("Fs");
            String Ss = req.getParameter("Ss");
            String lidS = req.getParameter("lid");
            System.out.println("dsadasd::" + uid + "  " + Fs + " " + Ss + " " +type);
             int lid = locationService.addLocation(Fs,Ss,lidS);
             resp.setCharacterEncoding("UTF-8");
             resp.setContentType("text/plain");
             resp.getWriter().print(lid);
        }
        else{
            int locationId = Integer.parseInt(req.getParameter("lid"));
            locationService.deleteLocation(locationId);
        }
    }
}
